package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.lemeng.dao.SchoolTypeMapper;
import com.lemeng.entity.PageResult;
import com.lemeng.pojo.SchoolType;
import com.lemeng.service.SchoolTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class SchoolTypeServiceImpl implements SchoolTypeService {

    @Autowired
    SchoolTypeMapper schoolTypeMapper;

    @Override
    public List<SchoolType> findAll() {
        return schoolTypeMapper.selectAll();
    }

    @Override
    public PageResult<SchoolType> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        PageInfo<SchoolType> schoolTypePageInfo = new PageInfo<>(schoolTypeMapper.selectAll());
//        Page<SchoolType> schoolTypes = (Page<SchoolType>)schoolTypeMapper.selectAll();
        return new PageResult(schoolTypePageInfo.getTotal(),schoolTypePageInfo.getList());
    }

    @Override
    public List<SchoolType> findList(Map<String, Object> serchMap) {
        Example example = new Example(SchoolType.class);
        Example.Criteria criteria = example.createCriteria();
        if (serchMap != null) {
            if (serchMap.get("name")!=null && StringUtil.isNotEmpty(serchMap.get("name").toString())){
                criteria.andLike("name","%"+serchMap.get("name").toString()+"%");
            }
            if (serchMap.get("letter")!=null && StringUtil.isNotEmpty(serchMap.get("letter").toString())){
                criteria.andEqualTo("letter",serchMap.get("letter").toString());
            }
        }
        return schoolTypeMapper.selectByExample(example);
    }

    @Override
    public PageResult<SchoolType> findPage(Integer page, Integer size, Map<String, Object> serchMap) {
        PageHelper.startPage(page,size);
        PageInfo<SchoolType> schoolTypePageInfo = new PageInfo<>(findList(serchMap));
        return new PageResult(schoolTypePageInfo.getTotal(),schoolTypePageInfo.getList());
    }

    @Override
    public SchoolType findById(Integer id) {
        return schoolTypeMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(SchoolType schoolType) {
        schoolTypeMapper.insert(schoolType);
    }

    @Override
    public void update(SchoolType schoolType) {
        schoolTypeMapper.updateByPrimaryKeySelective(schoolType);
    }

    @Override
    public void delete(Integer id) {
        schoolTypeMapper.deleteByPrimaryKey(id);
    }


}
